import 'package:get/get.dart';
import '/common/net/dio_utils.dart';

import 'entity/page_state.dart';
import 'entity/res_error_entity.dart';

class BaseController extends GetxController {
  ///错误信息展示
  final resErrorEntity = ResErrorEntity(1, "").obs;
  Worker? resErrorWorker;

  void resErrorData(errorDataCallBack) {
    resErrorWorker ??= ever<ResErrorEntity>(resErrorEntity, errorDataCallBack);
  }

  ///控制加载栏
  final _pageSateController = PageState.DEFAULT.obs;

  PageState get pageState => _pageSateController.value;

  updatePageState(PageState state) => _pageSateController(state);

  resetPageState() => _pageSateController(PageState.DEFAULT);

  showLoading() => updatePageState(PageState.LOADING);

  hideLoading() => resetPageState();

  ///Dio网络请求
  //get
  void getApi<T>(url,
      {NetSuccessCallback<T?>? onSuccess, NetErrorCallback? onError}) {
    DioUtils.instance.asyncRequestNetwork<T>(
      Method.get,
      url,
      onSuccess: onSuccess,
      onError: onError,
    );
  }

  //带参数的get请求
  void getQueryParametersApi<T>(url, Map<String, dynamic> queryParameters,
      {NetSuccessCallback<T?>? onSuccess, NetErrorCallback? onError}) {
    DioUtils.instance.asyncRequestNetwork<T>(
      Method.get,
      url,
      queryParameters: queryParameters,
      onSuccess: onSuccess,
      onError: onError,
    );
  }

  //post请求
  void postApi<T>(url, Map<String, dynamic> queryParameters,
      {Object? params,
      NetSuccessCallback<T?>? onSuccess,
      NetErrorCallback? onError}) {
    DioUtils.instance.asyncRequestNetwork<T>(
      Method.post,
      url,
      queryParameters: queryParameters,
      onSuccess: onSuccess,
      onError: onError,
    );
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    _pageSateController.close();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    resErrorWorker?.dispose();
  }
}
